package com.wn.authentication.web;

import com.wn.authentication.mapper.MenuRoleMapper;
import com.wn.authentication.mapper.RoleMapper;
import com.wn.authentication.service.UserService;
import com.wn.common.ResponseEntity;
import com.wn.entity.Menu;
import com.wn.entity.User;
import com.wn.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author wuqingting
 * @date 2022/10/25
 */
@RestController
@RequestMapping("/auth")
public class Controller {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    private JwtUtils jwtUtils=new JwtUtils();

    @Autowired
    private RoleMapper roleMapper;
    @RequestMapping("/login")
    public ResponseEntity login(@RequestBody User user){
        user=userService.login(user);
        if (user==null){
            return ResponseEntity.fail;
        }
        String token = jwtUtils.createJWT(user.getId(), user.getHr().getWorkNumber());
        //将菜单转换为树状
        List<Menu> menuList = user.getRole().getMenuList();
        List<Menu> menus = streamToTree1(menuList, 0);
        user.getRole().setMenuList(menus);
        //token存入redis,用于判断过期和续期
        //user存入redis,用于查询权限和菜单列表
        redisTemplate.opsForValue().set("token"+user.getId(),token,30, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set("user"+user.getId(),user,30, TimeUnit.MINUTES);

        return new ResponseEntity(token);
    }

    @RequestMapping("/getMenu")
    public ResponseEntity getMenu(HttpServletRequest request){
        String token = request.getHeader("token");
        int id = (int) jwtUtils.getInfo(token, "id");
        User user = (User) redisTemplate.opsForValue().get("user" + id);

        return new ResponseEntity(user.getRole().getMenuList());
    }

    private List<Menu> streamToTree1(List<Menu> treeList, int parentId) {
        //利用递归将list转换为树状
        List<Menu> list = treeList.stream()
                // 过滤父节点
                .filter(parent -> parent.getPid() == parentId)
                // 把父节点children递归赋值成为子节点
                .map(child -> {
                    child.setChildrenMenu(streamToTree1(treeList, child.getId()));
                    return child;
                }).collect(Collectors.toList());//将流中所有元素导入一个列表
        return list;
    }

    @Autowired
    private MenuRoleMapper menuRoleMapper;
    @RequestMapping("/dis/{id}")
    public ResponseEntity distribution(@PathVariable int id){
        List<Menu> allMenu = menuRoleMapper.getAllMenu();
        for (Menu menu : allMenu) {
            menuRoleMapper.add(id,menu.getId());
        }

        return  ResponseEntity.success;
    }
}
